<?php

set_include_path(get_include_path() . PATH_SEPARATOR . '..');

require_once 'components/utils/debug_utils.php';

Warning::BeginSuppress(PHPWarning::$Deprecated);

require_once 'simpletest/autorun.php';
require_once 'simpletest/mock_objects.php';

Warning::EndSuppress();

require_once 'database_engine/common_engine.php';
require_once 'database_engine/datareader_stub.php';
require_once 'components/vertical_grid.php';
require_once 'components/renderer.php';

require_once 'test_utils.php';

Mock::generate('IEditableDataRecord');

class VerticalGridTests extends UnitTestCase
{
    /**
     * @return SimpleMock|IEditableDataRecord
     */
    private function CreateMockDataWriter()
    {
        return new MockIEditableDataRecord();
    }

    /**
     * @return Renderer
     */
    private function CreateRenderer()
    {
        return new Renderer('components/templates', true);
    }

    public function testGridValues()
    {
        $dataWriter = new EditableDataReaderStub(
            array(
                array(
                    'id' => 1
                )
            )
        );
        $dataWriter->Open();
        $dataWriter->Next();

        $verticalGrid = new VerticalGrid($dataWriter);

        $row = $verticalGrid->AddFieldRow('Id', 'id');
        $rowProperties = new TextEditProperties('id_edit');
        $row->SetEditProperties($rowProperties);

        $this->assertEqual(1, $row->GetValue());

        $dataWriter->Close();
    }

    public function testRenderGridContainer()
    {
        $dataWriter = $this->CreateMockDataWriter();

        $verticalGrid = new VerticalGrid($dataWriter);
        $row = $verticalGrid->AddFieldRow('Id', 'id');
        $rowProperties = new TextEditProperties('id_edit');
        $row->SetEditProperties($rowProperties);

        $this->assertEqual(
            '<div class="pgui-vertical-grid"></div>',
            $verticalGrid->RenderGridContainer($this->CreateRenderer())
        );
    }

    public function testRenderFullGrid()
    {
        $dataWriter = $this->CreateMockDataWriter();

        $dataWriter->setReturnValue('GetFieldNames', array("id"));
        $dataWriter->setReturnValue('IsPrimaryKeyField', true);
        $dataWriter->setReturnValue('GetFieldValueByName', 1);

        $verticalGrid = new VerticalGrid($dataWriter);
        $row = $verticalGrid->AddFieldRow('Id', 'id');
        $rowProperties = new TextEditProperties('id_edit');
        $row->SetEditProperties($rowProperties);

        /*
        echo '<pre>';
        echo htmlspecialchars($verticalGrid->RenderFullGrid($this->CreateRenderer())) ;
        echo '</pre>';
         */
    }

    public function testSimpleUpdate()
    {
        $dataWriter = $this->CreateMockDataWriter();

        $dataWriter->setReturnValue('GetFieldNames', array("id", "value"));
        $dataWriter->setReturnValue('IsPrimaryKeyField', true, array("id"));
        $dataWriter->setReturnValue('IsPrimaryKeyField', false, array("value"));

        $verticalGrid = new VerticalGrid($dataWriter);
        $row = $verticalGrid->AddFieldRow('Id', 'id');
        $rowProperties = new TextEditProperties('id_edit');
        $row->SetEditProperties($rowProperties);

        $input = new InputValuesStub(array(
            'pk0' => 1,
            'id_edit' => '2'
        ));

        $primaryKeys = $verticalGrid->ExtractPrimaryKeyValuesFromPost($input);

        $this->assertEqual(1, count($primaryKeys));
        $this->assertEqual(1, $primaryKeys['id']);

        $verticalGrid->ProcessPost($input);

        $dataWriter->expectAtLeastOnce('SetFieldValueByName', array('id', 2));
    }
}

?>